package cn.wfu.rlrjds.app.user.controller;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.wfu.rlrjds.app.log.domain.Log;
import cn.wfu.rlrjds.app.log.service.ILogService;
import cn.wfu.rlrjds.app.repairman.service.ipml.RepairmanException;
import cn.wfu.rlrjds.app.user.domain.User;
import cn.wfu.rlrjds.app.user.service.IUserService;
import cn.wfu.rlrjds.app.user.service.ipml.UserEcxeption;
import cn.wfu.rlrjds.container.ServiceProvinder;
import cn.wfu.rlrjds.frame.utils.Const;
import cn.wfu.rlrjds.utils.GsonUtils;
import cn.wfu.rlrjds.utils.ImageUrlUtil;
import cn.wfu.rlrjds.utils.IpUtil;
import cn.wfu.rlrjds.utils.UUIDUtil;

/**
 * 用户控制器
 * 
 * @author MGL
 * 
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {
	@Autowired
	private IUserService userService;
	@Autowired
	private ILogService logService;

	/**
	 * 用户注册
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/registUser")
	@ResponseBody
	public String regist(HttpServletRequest request) {
		String userStr = request.getParameter("user");
		System.out.println(userStr);
		User form = GsonUtils.fromJson(userStr, User.class);
		String phone = form.getPhone();
		if (!phone.matches("^1[3|4|5|7|8]\\d{9}$")) {
			return Const.PHONE_EXIST;
		}
		try {
			userService.regist(form);
		} catch (UserEcxeption e) {
			// TODO Auto-generated catch block
			return Const.NAME_EXIST;
		}
		return Const.SUCCESS;
	}

	/**
	 * 用户登录
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/loginUser", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String login(HttpServletRequest request) {
		try {
			String uid = request.getParameter("uid");
			String ip = IpUtil.getIpAddr(request);
			User user = userService.findUserById(uid);
			Log log = new Log();
			log.setIp(ip);
			log.setLogDate(new Date());
			log.setLogId(UUIDUtil.uuid());
			log.setUserName(user.getUid());
			log.setType(0);
			logService.saveLog(log);
			return GsonUtils.toJson(user);
		} catch (UserEcxeption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 用户信息修改
	 * 
	 * @throws Exception
	 * 
	 */
	@RequestMapping(value = "/updateInfo", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String updateInfo(HttpServletRequest request) throws Exception {
		String jsonUserStr = request.getParameter("user");
		String uid = request.getParameter("uid");
		String ischangeavatar = request.getParameter("ischangeavatar");
		String basepath = request.getServletContext().getRealPath(
				ImageUrlUtil.IMAGE_BASE);
		String saveDir = basepath + "/" + uid + "/avatar";
		System.out.println("basepath" + basepath);
		File baseFile = new File(basepath);
		User user = GsonUtils.fromJson(jsonUserStr, User.class);
		user.setUid(uid);
		String phone = user.getPhone();
		if (!phone.matches("^1[3|4|5|7|8]\\d{9}$")) {
			return Const.PHONE_EXIST;
		}
		// 创建文件路径
		File file = new File(saveDir);
		if (!file.exists()) {
			file.mkdirs();
		}
		// 保存文件，设置头像路径
		if (ischangeavatar.equals("true") && user.getAvatarUrl() != null
				&& StringUtils.isNotBlank(user.getAvatarUrl())) {
			String avatarUrl = ImageUrlUtil.getImageURI(user.getAvatarUrl(),
					uid, saveDir);
			user.setAvatarUrl(avatarUrl);
		}
		User updateUser = this.userService.updateUserInfo(user,
				baseFile.getParent());
		String jsonUpdateUserStr = GsonUtils.toJson(updateUser);
		return jsonUpdateUserStr;
	}

	/**
	 * 修改用户密码
	 * 
	 * @param request
	 * @return
	 * 
	 */
	@RequestMapping(value = "/updatePassword", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String updatePassword(HttpServletRequest request) {
		String uid = request.getParameter("uid");
		String olderpass = request.getParameter("olderpass");
		String newpass = request.getParameter("newpass");
		User form = new User();
		form.setUid(uid);
		form.setPassword(olderpass);
		try {
			this.userService.updateUserPassword(form, newpass);
			return Const.SUCCESS;
		} catch (UserEcxeption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Const.NOT_FOUND;
		}

	}

	/**
	 * 通过uid查找用户
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/findUserByUId")
	@ResponseBody
	public String findUserByUId(HttpServletRequest request) {
		String uid = request.getParameter("uid");
		try {
			userService.findUserById(uid);
			return "true";
		} catch (UserEcxeption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "false";
		}
	}

	/**
	 * 通过查找所有用户
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/findAllUsers", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String findAllUsers(HttpServletRequest request) {
		List<User> users = userService.findAllUser(null);
		return GsonUtils.toJson(users);
	}
}
